using System;
using CommandSystem;
using RgCore.Database;

namespace RgCore.Commands.RgRACommands
{
    public class DatabaseState : ICommand
    {
        public bool Execute(ArraySegment<string> arguments, ICommandSender sender, out string response)
        {
            if (DatabaseController.DatabaseDisabled)
            {
                response = "数据库未开启";
                return true;
            }
            string text = "数据库连接池状态：";
            for (int i = 0; i < DatabaseController.Connections.Count; i++)
            {
                text +=
                    $"\n#{i + 1}: {(DatabaseController.Connections[i].Connection.State == System.Data.ConnectionState.Open ? "<color=#00FF00>开启</color>" : "<color=#FF0000>关闭</color>")}";
                text +=
                    $" {(DatabaseController.Connections[i].Occuiped ? "<color=yellow>(执行中)</color>" : "<color=blue>(空闲)</color>")}";
            }

            text += "\n线程状态：";
            text += $"\n{DatabaseController.KeepAliveThread.Name}：{DatabaseController.KeepAliveThread.ThreadState}";
            response = text;
            return true;
        }

        public string Command { get; } = "dbstate";
        public string[] Aliases { get; } = Array.Empty<string>();
        public string Description { get; } = "获取数据库状态";
    }
}